<ul class="site-nav list--inline {{ nav_alignment }}" id="SiteNav">
  {% for link in linklists[section.settings.main_linklist].links %}
    {%- assign child_list_handle = link.title | handleize -%}

    {% comment %}
      Check if third-level nav exists on each parent link.
    {% endcomment %}
    {%- assign three_level_nav = false -%}
    {% if link.links != blank %}
      {% if link.levels == 2 %}
        {%- assign three_level_nav = true -%}
      {% endif %}
    {% endif %}

    {% if link.links != blank %}
      <li class="site-nav--has-dropdown{% if three_level_nav %} site-nav--has-centered-dropdown{% endif %}{% if link.active %} site-nav--active{% endif %}" data-has-dropdowns>
        <button class="site-nav__link site-nav__link--main site-nav__link--button{% if link.child_active %} site-nav__link--active{% endif %}" type="button" aria-expanded="false" aria-controls="SiteNavLabel-{{ child_list_handle }}">
          <span class="site-nav__label">{{ link.title | escape }}</span>{% include 'icon-chevron-down' %}
        </button>

        <div class="site-nav__dropdown{% if three_level_nav %} site-nav__dropdown--centered{% endif %}" id="SiteNavLabel-{{ child_list_handle }}">
          {% if three_level_nav %}
            <div class="site-nav__childlist">
              <ul class="site-nav__childlist-grid">
                {% if link.links != blank %}
                  {% for childlink in link.links %}
                    <li class="site-nav__childlist-item">
                      <a href="{{ childlink.url }}"
                        class="site-nav__link site-nav__child-link site-nav__child-link--parent"
                        {% if childlink.current %} aria-current="page"{% endif %}
                      >
                        <span class="site-nav__label">{{ childlink.title | escape }}</span>
                      </a>

                      {% if childlink.links != blank %}
                        <ul>
                        {% for grandchildlink in childlink.links %}
                          <li>
                            <a href="{{ grandchildlink.url }}"
                            class="site-nav__link site-nav__child-link"
                            {% if grandchildlink.current %} aria-current="page"{% endif %}
                          >
                              <span class="site-nav__label">{{ grandchildlink.title | escape }}</span>
                            </a>
                          </li>
                        {% endfor %}
                        </ul>
                      {% endif %}

                    </li>
                  {% endfor %}
                {% endif %}
              </ul>
            </div>

          {% else %}
            <ul>
              {% for childlink in link.links %}
                <li>
                  <a href="{{ childlink.url }}"
                  class="site-nav__link site-nav__child-link{% if forloop.last %} site-nav__link--last{% endif %}"
                  {% if childlink.current %} aria-current="page"{% endif %}
                >
                    <span class="site-nav__label">{{ childlink.title | escape }}</span>
                  </a>
                </li>
              {% endfor %}
            </ul>
          {% endif %}
        </div>
      </li>
    {% else %}
      <li {% if link.active %} class="site-nav--active"{% endif %}>
        <a href="{{ link.url }}"
          class="site-nav__link site-nav__link--main{% if link.active %} site-nav__link--active{% endif %}"
          {% if link.current %} aria-current="page"{% endif %}
        >
          <span class="site-nav__label">{{ link.title | escape }}</span>
        </a>
      </li>
    {% endif %}
  {% endfor %}
</ul>
